package fr.ul.ade.prime.ui;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import fr.ul.ade.R;
import fr.ul.ade.prime.exception.AAAException;
import fr.ul.ade.prime.model.Project;
import fr.ul.ade.prime.model.Resource;
import fr.ul.ade.prime.model.ScheduleState;
import fr.ul.ade.prime.model.StudentNumber;
import fr.ul.ade.prime.provider.AdeWebContract;

/**
 * @author Schnell Michaël
 */
public class Bdd extends AdeWebActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setTheme(R.style.Theme_Sherlock);
		this.setContentView(R.layout.bdd);

		// final Bdd activity = this ;
		Button button = (Button) this.findViewById(R.id.clear_database);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// final Cursor cursor =
				// Bdd.this.getContentResolver().query(AdeWebContract.Schedule.CONTENT_URI,
				// new String[]{AdeWebContract.Schedule._ID}, null, null,
				// AdeWebContract.Schedule.DEFAULT_SORT) ;
				// while(cursor.moveToNext()) {
				// Bdd.this.service.removeSchedule(cursor.getLong(0)) ;
				// }// while
				// cursor.close() ;
				Bdd.this.getContentResolver()
						.delete(AdeWebContract.Schedule.CONTENT_URI, null, null);

				Toast.makeText(Bdd.this, "Emplois du temps suprimés.", //$NON-NLS-1$
						Toast.LENGTH_SHORT).show();
			}// onClick(View)
		});

		button = (Button) this.findViewById(R.id.fill_database);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Resource td1 = new Resource("11626") ;
				// Resource tp1 = new Resource("19382|19383") ;
				// Resource td2 = new Resource("11629") ;
				// Resource tp2 = new Resource("19385") ;
				// Resource robotique = new Resource("19388") ;
				// Resource ihm = new Resource("18688") ;
				// Resource apprentissage = new Resource("18694") ;
				// Resource cryptologie = new Resource("11629") ;
				// Resource infographie = new Resource("18686") ;

				Resource m1s2 = new Resource(
						"6180|6235|2815|6251|6281|7649|7786|21888|21889"); //$NON-NLS-1$
				String name = "Master 1 Semestre 2"; //$NON-NLS-1$
				Resource[] resources = new Resource[] { m1s2 };
				int period = 200;
				Project project = new Project(2, 5, "Projet principal"); //$NON-NLS-1$
				try {
					Bdd.this.service.createSchedule(name,
							resources,
							period,
							project);
				} catch (AAAException e) {
					Toast.makeText(Bdd.this, e.getMessage(), Toast.LENGTH_LONG)
							.show();
				}

				// name = "Sébastien" ;
				// resources = new Resource[]{td2, tp2, ihm, robotique} ;
				// period = 200 ;
				// project = new Project(2, 5, "Projet principal") ;
				// try {
				// Bdd.this.service.createSchedule(name, resources, period,
				// project) ;
				// } catch (AAAException e) {
				// Toast.makeText(Bdd.this, e.getMessage(),
				// Toast.LENGTH_LONG).show() ;
				// }
				//
				// name = "Victor" ;
				// resources = new Resource[]{td2, tp2, infographie, robotique}
				// ;
				// period = 200 ;
				// project = new Project(2, 5, "Projet principal") ;
				// try {
				// Bdd.this.service.createSchedule(name, resources, period,
				// project) ;
				// } catch (AAAException e) {
				// Toast.makeText(Bdd.this, e.getMessage(),
				// Toast.LENGTH_LONG).show() ;
				// }
				//
				// name = "Romain" ;
				// resources = new Resource[]{td1, tp1, apprentissage,
				// robotique} ;
				// period = 200 ;
				// project = new Project(2, 5, "Projet principal") ;
				// try {
				// Bdd.this.service.createSchedule(name, resources, period,
				// project) ;
				// } catch (AAAException e) {
				// Toast.makeText(Bdd.this, e.getMessage(),
				// Toast.LENGTH_LONG).show() ;
				// }
				//
				// name = "Master S1 (complet)" ;
				// resources = new Resource[]{td1, tp1, td2, tp2, ihm,
				// robotique, apprentissage, infographie, cryptologie} ;
				// period = 200 ;
				// project = new Project(2, 5, "Projet principal") ;
				// try {
				// Bdd.this.service.createSchedule(name, resources, period,
				// project) ;
				// } catch (AAAException e) {
				// Toast.makeText(Bdd.this, e.getMessage(),
				// Toast.LENGTH_LONG).show() ;
				// }

				name = "Test NoEtudiant"; //$NON-NLS-1$
				resources = new Resource[] { new StudentNumber("31011672", "") }; //$NON-NLS-1$ //$NON-NLS-2$
				period = 200;
				project = new Project(2, 5, "Projet principal"); //$NON-NLS-1$
				try {
					Bdd.this.service.createSchedule(name,
							resources,
							period,
							project);
				} catch (AAAException e) {
					Toast.makeText(Bdd.this, e.getMessage(), Toast.LENGTH_LONG)
							.show();
				}

				name = "Salles Infos"; //$NON-NLS-1$
				resources = new Resource[] { new Resource(
						"11196|11200|11445|11364|11367|11371|11377|11378|11379") }; //$NON-NLS-1$
				period = 28;
				project = new Project(2, 5, "Projet principal"); //$NON-NLS-1$
				try {
					Bdd.this.service.createSchedule(name,
							resources,
							period,
							project);
				} catch (AAAException e) {
					Toast.makeText(Bdd.this, e.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			}
		});

		button = (Button) this.findViewById(R.id.reset_states);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ContentValues values = new ContentValues();
				values.put(AdeWebContract.Schedule.SCHEDULE_STATUS,
						Integer.valueOf(ScheduleState.OTHER.ordinal()));
				Bdd.this.getContentResolver()
						.update(AdeWebContract.Schedule.CONTENT_URI,
								values,
								null,
								null);
				Bdd.this.service.clearUpdateQueue();

				Toast.makeText(Bdd.this, "Etats réinitialisés.", //$NON-NLS-1$
						Toast.LENGTH_SHORT).show();
			}
		});

		// button = (Button) this.findViewById(R.id.test_schedule) ;
		// button.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// new Thread(new Runnable() {
		// @Override
		// public void run() {
		// try {
		// String name = "Test EDT" ;
		// Resource[] ress = {
		// new Resource("11626"),
		// new Resource("19382|19383"),
		// new Resource("11629"),
		// new Resource("19385"),
		// new Resource("19388"),
		// new Resource("18688"),
		// new Resource("18694"),
		// new Resource("11629"),
		// new Resource("18686")
		// } ;
		// String resources = TextUtils.join("|", ress) ;
		// int period = 365 ;
		// Project project = new Project(2, 5, "Projet principal") ;
		// Schedule schedule = new Schedule(name, ress, period, project) ;
		//
		// Database db = new Database(Bdd.this) ;
		// db.open() ;
		// db.insertSchedule(schedule) ;
		//
		// Feed feed = ContainerData.getFeedConnexion() ;
		// String feedId = feed.getId() ;
		// feed = ContainerData.getFeedSetProject(feedId,
		// project.getAdeProjectId()) ;
		// List<FeedLesson> feeds = ContainerData.getFeedsLesson(feedId,
		// resources, period) ;
		// feed = ContainerData.getFeedDeconnexion(feedId) ;
		// FeedAnalyzer analyzer = new FeedAnalyzer(schedule, feeds) ;
		// analyzer.start() ;
		// String previousRooms = "" ;
		// String previousDate = "2012/11/12", previousStart = "10:00",
		// previousEnd = "12:00" ;
		// Random rnd = new Random() ;
		// boolean insertHistory = false ;
		// for(Lesson lesson : schedule.getLessons()) {
		// insertHistory = (rnd.nextInt() % 3 == 0) ;
		// if (insertHistory) {
		// lesson.setChange(Lesson.BIG_CHANGE) ;
		// }
		// if (rnd.nextInt() % 4 == 0) {
		// previousRooms = lesson.getRoom() ;
		// previousDate = lesson.getDate() ;
		// previousStart = lesson.getStartHour() ;
		// previousEnd = lesson.getEndHour() ;
		// }
		// long lessonId = db.insertLesson(lesson) ;
		// if (insertHistory) {
		// int caseHistory = rnd.nextInt() ;
		// // Important change
		// LessonHistory history = new LessonHistory(
		// lessonId, previousRooms,
		// previousDate, previousStart, previousEnd) ;
		// // Added rooms
		// if (caseHistory % 3 == 0) {
		// history.setRooms("") ;
		// }// Removed rooms
		// else if (caseHistory % 3 == 1) {
		// history.setRooms(history.getRooms() + "|Ancienne") ;
		// }
		// db.insertLessonHistory(history) ;
		// }
		// }
		// db.close() ;
		// } catch (Exception e) {
		// Log.d(TAG, "Failed to create test schedule.", e) ;
		// }
		// Toast.makeText(Bdd.this,
		//								"Emplois du temps de test crée.",  //$NON-NLS-1$
		// Toast.LENGTH_SHORT).show() ;
		//
		// }
		// }).start() ;
		// }
		// }) ;
	}// onCreate(Bundle)

}// class Bdd